package com.zygk.locationtool.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.zygk.library.base.BaseActivity;
import com.zygk.locationtool.Application;
import com.zygk.locationtool.R;
import com.zygk.locationtool.adapter.HomeProjectAdapter;
import com.zygk.locationtool.dao.CommonLogic;
import com.zygk.locationtool.model.M_Project;
import com.zygk.locationtool.model.apimodel.APIM_ProjectList;
import com.zygk.locationtool.util.HttpRequest;

/**
 * Created by Wikison on 2018/12/7.
 * 项目列表
 */
public class HomeActivity extends BaseActivity {
  @BindView(R.id.gv_project) GridView gvProject;

  private HomeProjectAdapter adapter;

  @Override public void setContentView() {
    setContentView(R.layout.activity_home);
  }

  @Override public void init() {
    initData();
    initView();
    initListener();
  }

  private void initData() {
    getHomeProject();
  }

  private void initView() {

  }

  private void initListener() {
    gvProject.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        M_Project m = adapter.getItem(position);
        Application.getInstance().API_URL = m.getAPIUrl();
        Intent intent = new Intent(mContext, StationListActivity.class);
        startActivity(intent);
      }
    });
  }

  private void getHomeProject() {
    CommonLogic.getBaseProjectsList(mContext, new HttpRequest.HttpCallback() {
      @Override public void onStart() {
        showPd();
      }

      @Override public void onSucceed(Object obj) {
        APIM_ProjectList result = (APIM_ProjectList) obj;
        adapter = new HomeProjectAdapter(mContext, result.getResultObj());
        gvProject.setAdapter(adapter);
      }

      @Override public void onFailed() {

      }

      @Override public void onFinish() {
        dismissPd();
      }
    });
  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ButterKnife.bind(this);
  }
}
